package com.briup.product_my.service.impl;

import com.briup.product_my.domain.ManagerFenceHouse;
import com.briup.product_my.domain.ext.ManagerFenceHouseExt;
import com.briup.product_my.mapper.ManagerFenceHouseMapper;
import com.briup.product_my.result.Result;
import com.briup.product_my.service.FenceHouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.briup.product_my.mapper.ext.ManagerFenceHouseExtMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/8/21-08-21-下午6:32
 * @Description: com.briup.product_my.service.impl
 */
@Service
public class FenceHouseImpl implements FenceHouseService {
    @Autowired
    private ManagerFenceHouseMapper managerFenceHouseMapper;
    @Autowired
    private ManagerFenceHouseExtMapper managerFenceHouseExtMapper;
    @Override
    public PageInfo<ManagerFenceHouse> findAll(String fhName, Integer pageNum, Integer pageSize) {
        // PageHelper开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 核心查询语句
        List<ManagerFenceHouse> managerFenceHouses = managerFenceHouseMapper.findAll(fhName);
        // 将查询的信息封装在PageInfo对象中，返回
        PageInfo<ManagerFenceHouse> pageInfo = new PageInfo<>(managerFenceHouses);
        return pageInfo;
    }

    @Override
    public void saveOrUpdate(ManagerFenceHouse managerFenceHouse) {
        if (managerFenceHouse.getFhId() == null) {
            managerFenceHouse.setFhId(managerFenceHouseMapper.getMaxId() + 1);
            managerFenceHouseMapper.insert(managerFenceHouse);
        } else {
            managerFenceHouseMapper.update(managerFenceHouse);
        }
    }

    @Override
    public Result delete(String id) {
        ManagerFenceHouseExt hurdlesById = managerFenceHouseExtMapper.findHurdlesById(id);
        if(hurdlesById.getMhs().isEmpty()){
            managerFenceHouseMapper.delete(id);
            return Result.success();
        }else {
            return Result.failure( 0,"该围栏下存在设备，不能删除");
        }

    }

    @Override
    public Result deleteByIdAll(String[] ids) {
//        managerFenceHouseMapper.deleteByIdAll(ids);
        for (int i = 0; i < ids.length; i++) {
            ManagerFenceHouseExt hurdlesById = managerFenceHouseExtMapper.findHurdlesById(ids[i]);
            if(hurdlesById.getMhs().isEmpty()){
                managerFenceHouseMapper.delete(ids[i]);
            }
        }
        return Result.success();

    }

    @Override
    public ManagerFenceHouseExt findHurdlesById(String fhId) {
       return managerFenceHouseExtMapper.findHurdlesById(fhId);

    }

    @Override
    public List<ManagerFenceHouse> queryAll() {
        return managerFenceHouseMapper.queryAll();
    }
}
